package swing.dialog;

import api.UserApi;
import beans.Meaning;
import client.UserSession;
import http.Response;
import model.ApiResultModel;
import myException.ServerConnectException;

import javax.swing.*;
import java.awt.*;

/**
 * 单词释义详情对话框
 */
public class MeaningDetailDialog extends JDialog {

    public JPanel meaningDetailPanel;

    public JLabel meaningStaticText;
    public JLabel meaningText;
    public JLabel userStaticText;
    public JLabel createUser;
    public JLabel timeStaticText;
    public JLabel createTime;
    public JLabel updateStaticText;
    public JLabel updateUser;
    public JLabel updateTimeStaticText;
    public JLabel updateTime;

    public Meaning meaning;

    public MeaningDetailDialog(JFrame parentFrame, Meaning meaning) {
        super(parentFrame, false);
        this.meaning = meaning;
        this.setTitle("单词释义详情");
        this.setSize(350, 430);
        this.setLocationRelativeTo(parentFrame);
        meaningDetailPanel = new JPanel();
        meaningDetailPanel.setLayout(new BoxLayout(meaningDetailPanel, BoxLayout.Y_AXIS));
        initAllComponent();
        addAllComponent();
        this.setContentPane(meaningDetailPanel);
    }

    private void initAllComponent() {
        //单词释义 静态文本
        meaningStaticText = new JLabel(" 单词释义：");
        Font staticTextFont = new Font("宋体", Font.BOLD,20);
        meaningStaticText.setFont(staticTextFont);
        meaningStaticText.setAlignmentX(0.11f);
        //单词释义
        String meaningStr = "<html>" + meaning.getWord_meaning() + "</html>";
        meaningText = new JLabel(meaningStr);
        Font font = new Font("宋体", Font.PLAIN,17);
        meaningText.setFont(font);
        //创建者 静态文本
        userStaticText = new JLabel("创建者：");
        userStaticText.setFont(staticTextFont);
        //单词创建者
        createUser = new JLabel();
        String userName = queryUserName(meaning.getCreate_user());
        createUser.setText(userName);
        createUser.setFont(font);
        //创建时间 静态文本
        timeStaticText = new JLabel("创建时间：");
        timeStaticText.setFont(staticTextFont);
        //单词创建时间
        createTime = new JLabel(meaning.getCreate_time().toString());
        createTime.setFont(font);
        if (meaning.getUpdate_user() > 0) {
            //最近更新操作者 静态文本
            updateStaticText = new JLabel("最近更新操作者：");
            updateStaticText.setFont(staticTextFont);
            //单词创建者
            updateUser = new JLabel();
            String updateUserName = queryUserName(meaning.getUpdate_user());
            updateUser.setText(updateUserName);
            updateUser.setFont(font);
            //最近更新时间 静态文本
            updateTimeStaticText = new JLabel("最近更新时间：");
            updateTimeStaticText.setFont(staticTextFont);
            //单词更新时间
            updateTime = new JLabel(meaning.getUpdate_time().toString());
            updateTime.setFont(font);
        }
    }

    private void addAllComponent() {
        //重构，使用一个Jpanel包裹起来
        meaningDetailPanel.add(new JLabel("  "));
        meaningDetailPanel.add(meaningStaticText);
        meaningDetailPanel.add(meaningText);
        meaningDetailPanel.add(new JLabel("  "));
        meaningDetailPanel.add(userStaticText);
        meaningDetailPanel.add(createUser);
        meaningDetailPanel.add(new JLabel("  "));
        meaningDetailPanel.add(timeStaticText);
        meaningDetailPanel.add(createTime);
        meaningDetailPanel.add(new JLabel("  "));
        if (meaning.getUpdate_user() > 0) {
            meaningDetailPanel.add(updateStaticText);
            meaningDetailPanel.add(updateUser);
            meaningDetailPanel.add(new JLabel("  "));
            meaningDetailPanel.add(updateTimeStaticText);
            meaningDetailPanel.add(updateTime);
            meaningDetailPanel.add(new JLabel("  "));
        }
    }

    public String queryUserName(long query_user) {
        try {
            Response response = UserApi.query_user_name(UserSession.LoginUser(), query_user);
            ApiResultModel apiResultModel = ApiResultModel.fromJson(response.getBody());
            if (apiResultModel.getCode() == ApiResultModel.SUCCESS_CODE) {
                return  (String)apiResultModel.getResult();
            }
        } catch (ServerConnectException e) {
            showMessageDialog("服务器连接失败");
            this.dispose();
            e.printStackTrace();
        }
        return null;
    }

    private void showMessageDialog(String message) {
        JOptionPane.showMessageDialog(meaningDetailPanel, message);
    }
}
